package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Emp;
import org.example.pojo.Result;
import org.example.service.EmpService;
import org.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 包名：org.example.controller
 * 用户：admin
 * 日期：2025-09-14
 * 项目名称：pro-demo
 */
@RestController
@Slf4j
public class LoginController {

    @Autowired
    private EmpService empService;
    @Autowired
    private JwtUtils jwtUtils;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        log.info("员工登录：{}",emp);
        Emp e = empService.login(emp);
        //赋予令牌
        if(e!=null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", e.getId());
            claims.put("name", e.getName());
            claims.put("username",e.getUsername());

            String jwt = jwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }


        return Result.error("用户名或密码错误");
    }
}
